顯示具有 pthread 標籤的文章。 顯示所有文章
顯示具有 pthread 標籤的文章。 顯示所有文章

2014年6月28日 星期六

[pthread] Multi pthread






Code


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 5

void *thread_function(void *arg);

int main() {
int res;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
int lots_of_threads;

for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {

res
= pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads);
if (res != 0) {
perror
("Thread creation failed");
exit(EXIT_FAILURE);
}
sleep
(1);
}

/* Recycle each of pthread */
printf
("Waiting for threads to finish...\n");
for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
res
= pthread_join(a_thread[lots_of_threads], &thread_result);
if (res == 0) {
printf
("Picked up a thread\n");
}
else {
perror
("pthread_join failed");
}
}
printf
("All done\n");
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
int my_number = *(int *)arg;
int rand_num;

printf
("thread_function is running. Argument was %d\n", my_number);
rand_num
=1+(int)(9.0*rand()/(RAND_MAX+1.0));
sleep
(rand_num);
printf
("Bye from %d\n", my_number);
pthread_exit
(NULL);
}


Result


thread_function is running. Argument was 0
thread_function
is running. Argument was 1
thread_function
is running. Argument was 2
thread_function
is running. Argument was 3
thread_function
is running. Argument was 4
Bye from 1
Waiting for threads to finish...
Bye from 0
Bye from 2
Bye from 3
Bye from 4
Picked up a thread
Picked up a thread
Picked up a thread
Picked up a thread
Picked up a thread
All done

2014年6月27日 星期五

[Pthread][mutex] Pthread + mutex to create synchronize


mutex (programming) 1

Code

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* doSomeThing(void *arg)
{
    pthread_mutex_lock(&lock);

    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++);

    printf("\n Job %d finished\n", counter);

    pthread_mutex_unlock(&lock);

    return NULL;
}
int main(void)
{
    int i = 0;
    int err;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }
/* Create two pthread */
    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }
/* pthread 0 go doSomeThing first, then pthread 1 */
    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_mutex_destroy(&lock);

    return 0;
}

Result


 Job 1 started
 Job 1 finished
 Job 2 started
 Job 2 finished

Reference


[Pthread][Semaphore] Pthread + semaphore to create synchronize



Semaphore (programming) 1






C code


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void *thread_function(void *arg);
sem_t bin_sem;

#define WORK_SIZE 1024
char work_area[WORK_SIZE];

int main() {
int res;
pthread_t a_thread;
void *thread_result;

/* Initial Semaphore 0 */
res
= sem_init(&bin_sem, 0, 0);
if (res != 0) {
perror
("Semaphore initialization failed");
exit(EXIT_FAILURE);
}
res
= pthread_create(&a_thread, NULL, thread_function, NULL);
if (res != 0) {
perror
("Thread creation failed");
exit(EXIT_FAILURE);
}

printf
("Input some text. Enter 'end' to finish\n");
while(strncmp("end", work_area, 3) != 0) {
fgets
(work_area, WORK_SIZE, stdin);
sem_post
(&bin_sem); /* Increase Semaphore to 1 ( 0 + 1 ) */
}
printf
("\nWaiting for thread to finish...\n");
res
= pthread_join(a_thread, &thread_result);
if (res != 0) {
perror
("Thread join failed");
exit(EXIT_FAILURE);
}
printf
("Thread joined\n");
sem_destroy
(&bin_sem);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
sem_wait
(&bin_sem); /* decrease Semaphore to 0 ( 1 - 1 ) */
while(strncmp("end", work_area, 3) != 0) {
printf
("You input %d characters\n", strlen(work_area) -1);
sem_wait
(&bin_sem); /* wait for another Semaphore, because the value is 0 now */
}
pthread_exit
(NULL);
}


Result


Input some text. Enter 'end' to finish
This is my first pthread and semaphore.
You input 39 characters
end

Waiting for thread to finish...
Thread joined




Reference



[Pthread] Simple Pthread synchronize


The following code can download from [Linux Programer] 1

C code


/** 
 * 
 * Simple Pthread synchronize
 * 
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg);
int run_now = 1;
char message[] = "Hello World";
int main() {
    int res;
    pthread_t a_thread;
    void *thread_result;
    int print_count1 = 0;
/* Start to create pthread */
    res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
    if (res != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
/* one pthread go here */
    while(print_count1++ < 20) {
        if (run_now == 1) {
            printf("1");
            run_now = 2; /* When run_now set to 2, then wait for another pthread to execute */
        }
        else {
            sleep(1);
        }
    }
/* Finish pthread */
    printf("\nWaiting for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if (res != 0) {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread joined\n");
    exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) {
    int print_count2 = 0;

    while(print_count2++ < 20) {
        if (run_now == 2) {
            printf("2");
            run_now = 1;
        }
        else {
            sleep(1);
        }
    }

    sleep(3);
}

Result


We can understand pthread execute one by one. This synchronize code take too much time.


12121212121212
Waiting for thread to finish...
Thread joined

Reference