I am learning multithreading performance in C. When I tried to write a sample code, I bumped into a problem:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct{
int a;
char b;
} args;
void* some_func (void* arg)
{
args *argsa = malloc(sizeof(args));
//copy the content of arg to argsa,
//so changes to arg in main would not affect argsa
*argsa = *(args*) arg;
int i = 10;
for (; i > 0; i--)
{
usleep (1); //to give other threads chances to cut in
printf ("This is from the thread %d\n", argsa->a);
}
free (argsa);
}
int main()
{
pthread_t thread[3];
args ss;
int index = 0;
ss.b = 's';
for (; index <3 ; index++)
{
ss.a = index;
if (pthread_create (thread+index, NULL, some_func, (void*)&ss ))
{
usleep(10);
printf ("something is wrong creating the thread");
}
}
pthread_join ( thread[0], NULL);
pthread_join ( thread[1], NULL);
pthread_join ( thread[2], NULL);
return 0;
}
I know char b in the struct is useless, but I just want to practice passing a structure.
I expect the code to print out "This is from the thread x", where x is 0, 1 or 2, alternatively. However, the code currently only gives me "This is from the thread 2" 30 times. I believe there is something wrong with
*argsa = *(args*) arg;
But I can't find a way to solve this and get the desired output.
Any help would be appreciated!
Because you are passing the same pointer to all the threads. By the time thread 0 has started, you have already incremented the value of ss.a to 1 (and then 2).
This is a bit more correct:
void* some_func (void* arg)
{
args *argsa = (args*)arg;
int i;
for (i = 0; i < 10; i++)
{
usleep (1); //to give other threads chances to cut in
printf ("This is from the thread %d\n", argsa->a);
}
}
int main()
{
pthread_t thread[3];
args ss[3];
int index;
for (index = 0; index < 3; index++)
{
ss[index].a = index;
if (pthread_create(&thread[index], NULL, some_func, &ss[index] ))
{
printf ("something is wrong creating the thread");
}
}
pthread_join ( thread[0], NULL);
pthread_join ( thread[1], NULL);
pthread_join ( thread[2], NULL);
return 0;
}
The pattern to use to solve this kind of problem is as follows:
Create a structure that will hold the parameters you want to pass to the thread.
Allocate such a structure with malloc.
Fill in the structure.
Pass the pointer to the structure to the thread.
When the thread is finished with the structure, the thread frees it.
This assumes you don't need to get any information back from the thread. If you do, you can change it so that the code that joins the thread frees the structure. That allows the structure to hold a reply as well -- you join the thread, read the response information, and then free the structure.
No special locking or synchronization is required because while the newly-created thread exists, it is the only thread that touches the structure.
Sorry guys, but I was trying to solve the same issue and I don't think a proper answer was given yet, in order to solve the problem. I tried this on my own and I came up with the following code. Now, I compiled and run it and it pretty worked as I expected, still I not that confident that the "lock in main and unlock in child process" is the most elegant solution, so I'd like to know what you think about it. Thank you very much in advance for any clarification.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct{
int a;
char b;
} args;
pthread_mutex_t lock;
void* some_func (void *arg) {
args argsa = *(args*)arg;
pthread_mutex_unlock(&lock);
printf ("This is from the thread %d\n", argsa.a);
}
int main() {
pthread_t thread[10];
args ss;
int i, index=0;
ss.b = 's';
if (pthread_mutex_init(&lock, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
for (index = 0; index < 10 ; index++)
{
pthread_mutex_lock(&lock);
ss.a = index;
printf("index=%d, ", ss.a);
if (pthread_create (thread+index, NULL, some_func, (void*)&ss ))
{
usleep(10);
printf ("something is wrong creating the thread");
}
}
for(i=0;i<10;i++)
pthread_join ( thread[0], NULL);
return 0;
}
Output:
#./program
index=0, This is from the thread 0
index=1, This is from the thread 1
index=2, This is from the thread 2
index=3, This is from the thread 3
index=4, This is from the thread 4
index=5, This is from the thread 5
index=6, This is from the thread 6
index=7, This is from the thread 7
index=8, This is from the thread 8
index=9, This is from the thread 9
Related
pthread_create in a for loop, this is my code
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
pthread_mutex_t mutex;
void* helloWorld(void *i) {
pthread_mutex_lock(&mutex);
printf("This is a thread %d\n", *((int*) i));
pthread_mutex_unlock(&mutex);
return 0;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_t threads[4];
int i;
printf("Main Message\n");
for (i = 0; i < 4; i++) {
pthread_create(&threads[i], NULL, helloWorld, &i);
}
for (i = 0; i < 4; i++) {
pthread_join(threads[i], NULL);\
}
pthread_mutex_destroy(&mutex);
return 0;
}
The order doesn't really matter as long as all 4 threads are working.
I've tried to use mutex but it didn't solve the issue.
My current output is pretty random, it can be 0000 or 0112 or anything else.
The problem is two-fold:
First of all you don't have control over when the threads run or in which order.
You pass the same pointer to all threads.
You can solve the second issue by passing the value if i to the threads. This is one of the few cases where it's considered okay to pass values instead of pointers:
pthread_create(&threads[i], NULL, helloWorld, (void*)(uintptr_t) i);
Then in the thread
printf("This is a thread %d\n", (int)(uintptr_t) i);
The first issue, about the order, you have to come up with some other way to synchronize the threads and how they notify each other. For example by using four condition signals, one for each thread, that you signal in the order you want the threads to execute.
I have two functions initialize() and deinitialize() and each function should run only once. The structure is something similar to:
int *x;
initialize()
{
x = malloc(sizeof(int) * 10);
}
deinitialize()
{
free(x);
}
How can I ensure that only the first thread calls initialize and only the last thread calls deinitialize.
Can this be achieve without the use of mutex?
UPDATE:
Sorry for the poor information. I am actually modifying a library that contains the functions initialize() and deinitialize(). I need to make these two functions thread-safe. The users might use multiple threads and might call these functions more than once. I cannot assume that the users will call exactly once to initialize and deinitialize functions. I can only assume is that if a thread calls initialize, it will call deinitialize at some point.
The users will be using pthread library to create their different threads.
I don't know what you want to achieve, the most easiest is:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int *x;
int running;
void initialization(void)
{
x = malloc(sizeof(int) * 10);
puts("First thread init x");
}
void deinitialization(void)
{
free(x);
puts("Last thread reset x");
}
void *handler(void *data)
{
printf("Thread started\n");
while (running) {
do_work();
}
printf("Thread exit\n");
}
int main(void)
{
pthread_t threads[3];
initialization();
for (int i = 0; i < 3; i++)
pthread_create(&threads[i], NULL, &handler, NULL);
sleep(2);
running = 0;
for (int i = 0; i < 3; i++)
pthread_join(threads[i], NULL);
deinitialization();
return 0;
}
Here you can be sure that your have called init() and deinit() only once.
UPDATE
Another variant little bit complicated, but here you also can be sure that init() called only once. Thread with id 0 can be start after 1 in this case we should wait while *x is NULL.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX_THREADS 3
int *x;
int running;
int init;
struct thread_info
{
pthread_t thread;
int id;
int first_id;
int last_id;
};
void initialization(void)
{
x = malloc(sizeof(int) * 10);
puts("First thread init x");
init = 1;
}
void deinitialization(void)
{
free(x);
puts("Last thread reset x");
}
void *handler(void *data)
{
struct thread_info *tinfo = data;
printf("Thread started\n");
if (tinfo->id == 0)
initialization();
while (!init);
/* EMPTY BODY */
while (running) {
do_work();
}
printf("Thread exit\n");
}
int main(void)
{
struct thread_info threads[MAX_THREADS] =
{
[0 ... 2].id = -1,
[0 ... 2].first_id = 0,
[0 ... 2].last_id = (MAX_THREADS - 1)
};
for (int i = 0; i < 3; i++)
pthread_create(&threads[i].thread, NULL, &handler,
((threads[i].id = i), &(threads[i])));
sleep(2);
running = 0;
for (int i = 0; i < 3; i++)
pthread_join(threads[i].thread, NULL);
deinitialization();
return 0;
}
deinit() is a little bit tricky because your last thread can be exiting and free(x) while another thread is still running and maybe use it. So I leave it after all threads is exiting.
This is the point about concurrent programming. You can never make any assumptions about the order in which the threads execute.
The exact timing of when tasks in a concurrent system are executed depend on the scheduling, and tasks need not always be executed concurrently. For example, given two tasks, T1 and T2:
T1 may be executed and finished before T2 or vice versa (serial and
sequential)
T1 and T2 may be executed alternately (serial and concurrent)
T1 and T2 may be executed simultaneously at the same instant of time
(parallel and concurrent)
I've spent quite a few hours on trying to figure this one out and I'm completly stuck. The program is supposed to start 6 threads. Where some threads start where others end. Right now, I'm trying to get one single thread (thread 0) to execute. The caps lock commenting shows where I have added code and done my mistakes. My main struggle here is dealing with the pointers. Could anyone give me any pointers (ha..ha.. :c )?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define SHARED 1
sem_t sem[6];
struct threadargs
{
int id; /* thread number */
int sec; /* how many sec to sleep */
int signal[6]; /* which threads to signal when done */
};
void *tfunc(void *arg)
{
int i;
struct threadargs *targs=arg;
sem_wait(sem); //WAIT FOR OWN SEMAPHORE
printf("Thread %d is running\n", targs->id);
sleep(targs->sec);
printf("Thread %d is completed and may wake others..\n", targs->id);
for(i=0; i<6; i++) //ITERATE OVER signal_ARRAY &
{ //WAKE THREAD NUMBER i IF
if(targs->signal[i] == 1) //signal[i] IS 1
pthread_cond_signal(&sem[i]);
}
}
int main(void)
{
int i, j;
struct threadargs *targs[6];
pthread_t tid[6];
for(i=0; i<6; i++)
{
targs[i] = (struct threadargs*) malloc(sizeof(struct threadargs));
for(j=0; j<6; j++)
{ targs[i]->signal[j]=0; }
}
targs[0]->id=1;
targs[0]->sec=1;
targs[0]->signal[1]=1;
targs[0]->signal[4]=1;
sem[0] = 0; //INITIALIZE THREAD'S SEMAPHORE TO 0 or 1
pthread_create(targs[0], NULL, tfunc, NULL) // START THREAD
for(i=0; i<6; i++)
pthread_join(tid[i], NULL);
return 0;
}
Alright. First things first, I do recommend taking a second look at your coding style. It is of course highly subjective and I won't say yours is bad, but it took me a while to figure it out (if you really want to know, I recommend the Linux coding style for C/C++ code).
Lets get on with your problem. As far as I can see, the main issue seems that you're basically comparing pointers to apples with pointers to banana's (in other words, you're using the wrong pointer type in the wrong place).
To make sure that calls to functions and the like are correct, make sure to look up the API documentation for functions that are new to you (examples: pthread_create, sem_init, sem_wait, sem_post, pthread_cond_signal).
As you can see, pthread_cond_signal doesn't take a sem_t* as argument, and therefore you can't pass one to it and expect it to work. Below you'll find an example program showing how semaphores are used.
First, a new thread is created which will be put in waiting state instantly. As soon as the main tread finished counting from 0 to 150, it will post ('unlock') the semaphore and allowing the second thread to finish its execution.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static sem_t sem_thread_one;
static pthread_t thread_one_data;
static int x;
static void *tfunc(void *arg)
{
sem_wait(&sem_thread_one);
printf("Thread 1 is running. The value of x is %i\n", x);
return NULL;
}
int main(int argc, char **argv)
{
sem_init(&sem_thread_one, 0 /* don't share between processes */, 0);
if(pthread_create(&thread_one_data, NULL, &tfunc, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
while(x < 150) {
x++;
}
sem_post(&sem_thread_one);
if(pthread_join(thread_one_data, NULL)) {
fprintf(stderr, "Could not join threads, exiting!\n");
return -EXIT_FAILURE;
}
sem_destroy(&sem_thread_one);
printf("Program ran succesfully!\n");
return -EXIT_SUCCESS;
}
Save in a file sem.c and compile & link using:
gcc -Wall -Os -pthread -o sem_test sem.c
Now a second example, but now using pthread_cond_t. The functionality of the program is somewhat similar, it waits for a counter to reach a certain number.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
static pthread_t thread_one_data, thread_two_data;
static volatile int x, y, idx = 10;
static int count = 1;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
static void *cond_test_wait(void *arg)
{
pthread_mutex_lock(&mutex);
while(count < 10) {
printf("Waiting for `count < 10' to become true\n");
pthread_cond_wait(&condition, &mutex);
}
pthread_mutex_unlock(&mutex);
printf("Test wait thread finished. Value of count: %i\n", count);
return NULL;
}
static void *cond_test_signal(void *arg)
{
while(count < 10) {
pthread_mutex_lock(&mutex);
pthread_cond_signal(&condition);
/* do more intelligent things here */
count++;
pthread_mutex_unlock(&mutex);
}
printf("Test signal thread finished\n");
return NULL;
}
int main(int argc, char **argv)
{
if(pthread_create(&thread_one_data, NULL, &cond_test_wait, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
if(pthread_create(&thread_two_data, NULL, &cond_test_signal, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
pthread_join(thread_one_data, NULL);
pthread_join(thread_two_data, NULL);
pthread_cond_destroy(&condition);
pthread_mutex_destroy(&mutex);
printf("Program ran succesfully!\n");
return -EXIT_SUCCESS;
}
Save in a file cond.c and compile & link using:
gcc -o cond -pthread -Os -Wall cond.c
Do note how neat condition work in this example. You can use them to wait until any expression (= condition) becomes true. After the condition becomes true normal execution continue's.
If you need any more help, don't hesitate to ask in the comments. Good luck combining the above examples to fix up your program.
I am working a c- project which uses semaphores to handle the same function at the same time. When I run it, on linux, under the root user, it works perfect. But if I run it on another user, the script isn't executed and it leaves a semaphore in the semaphore array. Does anybody know a solution to this problem?
Here is my code:
int main(int argC, char* argv[]) {
pthread_t thr[argC-1];
int indexes[argC-1];
int i,j;
for(j=0; j<(argC-1); j++) {
indexes[j] = atoi(argv[j+1]);
pthread_create (&thr[j], NULL, (int *) &stabtest, (void *) &indexes[j]);
}
sem_init(&mutex, 0, 1);
for(j=0; j<(argC-1); j++) pthread_join(thr[j], NULL);
// Destroy semaphore
sem_destroy(&mutex);
// Exit
exit(0);
}
int stabtest(void *ptr) {
sem_wait(&mutex); // down semaphore
// Other code ...
sem_post(&mutex); // up semaphore
pthread_exit(0); // exit thread
}
This code I actually found on the internet because I have no experience with semaphores. So I am not sure of this code is the right one to run the function in parallel at the same time. But it works for the root user, so I guess the code is more or less ok.
Thank you!
If this is the code you are using, whatever you think is happening isn't happening. Permissions should have no effect on the semaphore you are using. And there is no semaphore array, you are using a single semaphore.
You seem to have a shaky grasp on command line parms. They are an array pointers to string. argc is the number of arguments. There will always be at least 1 argv, argv[0], which is the program name. Please don't rename argc. Everyone knows what argc is. Renaming it will just annoy people.
You start your threads and then you initialize your semaphore. That's a problem.
The start function of a pthread has to have the signature void* stabtest(void *ptr). Yours is int stabtest(void *ptr) and you are trying to cast your way out of a mistake. Don't do that. If you want to return something from a thread you do it through the void ptr used as the 4th parm in pthread_create. That is, allocate some memory, pass it in pthread_create, do whatever you need to in the thread to change the information pointed to, and then return the same void ptr from the thread. When you do your pthread_join you can access the returned pointer to the data in the second paramater in pthread_join.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
void* stabtest(void *ptr)
{
sem_wait(&mutex); // down semaphore
printf("in thread %lu...\n", pthread_self());
sem_post(&mutex); // up semaphore
pthread_exit(0); // exit thread
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("usage: %s numThread\n", argv[0]);
exit(1);
}
int maxThreads = atoi(argv[1]);
pthread_t thr[maxThreads];
int indexes[maxThreads];
int i, j;
sem_init(&mutex, 0, 1);
for (j = 0; j < maxThreads; j++)
pthread_create (&thr[j], NULL, stabtest, NULL);
for (j = 0; j < maxThreads; j++)
pthread_join(thr[j], NULL);
// Destroy semaphore
sem_destroy(&mutex);
// Exit
exit(0);
}
The problem is in following:
I want to write a short program that creates 10 threads and each prints a tread "id" that is passed to thread function by pointer.
Full code of the program is below:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct params {
pthread_mutex_t mutex;
int id;
};
typedef struct params params_t;
void* hello(void* arg){
int id;
pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
id = (*(params_t*)(arg)).id;
pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
printf("Hello from %d\n", id);
}
int main() {
pthread_t threads[10];
params_t params;
pthread_mutex_init (¶ms.mutex , NULL);
int i;
for(i = 0; i < 10; i++) {
params.id = i;
if(pthread_create(&threads[i], NULL, hello, ¶ms));
}
for(i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
The supposed output is (not necessary in this order):
Hello from 0
....
Hello from 9
Actual result is:
Hello from 2
Hello from 3
Hello from 3
Hello from 4
Hello from 5
Hello from 6
Hello from 8
Hello from 9
Hello from 9
Hello from 9
I tried to place mutex in different places in hello() function, but it didn't help.
How should I implement thread sync?
EDIT: Supposed result is not necessary 0...9 it can be any combination of these numbers, but each one should appear only one time.
The problem lies in the below code:
for(i = 0; i < 10; i++)
{
params.id = i;
if(pthread_create(&threads[i], NULL, hello, ¶ms));
}
Your params.id value keeps getting updated in the main thread, whereas you are passing the same pointer to all the threads.
Please create seperate memory for params by dynamically allocating it and pass it to different threads to solve the problem.
EDIT1:
Your usage of mutex to protect is also an incorrect idea. Though your mutex if used in main while setting the id also, may make the updation mutually exclusive, but you may not get your desired output. Instead of getting values from 0 .. 9 in different threads, you may get all 9s or still multiple threads may print same values.
So, using thread synchronization is not such a good idea for the output which you are expecting. If you still need to use one param variable between all threads and get output as 0 to 9 from each of the threads, better move the pthread_join into the first loop. This will ensure that each thread gets created, prints the value and then returns before the main spawns the next thread. In this case, you don't need the mutex also.
EDIT2:
As for the updated question, where it is asked that it is not necessary to print the numbers 0..9 in a sequence, the printing can be random, but only once, the problem still remains the same more or less.
Now, let's say, the value of params.id is first 0 and thread 0 got created, now, thread 0 must print it before it is updated in the main thread, else, when thread 0 accessess it, the value of params.id would have become 1 and you will never get your unique set of values. So, how to ensure that thread 0 prints it before it is updated in main, Two ways for it:
Ensure thread 0 completes execution and printing before main updates
the value
Use condition variables & signalling to ensure that main thread waits
for thread 0 to complete printing before it updates the value (Refer
to Arjun's answer below for more details)
In my honest opinion, you have selected the wrong problem for learning synchronization & shared memory. You can try this with some good problems like "Producer-Consumer", where you really need synchronization for things to work.
There are two problems:
A. You're using a lock but main is unaware of this lock.
B. A lock is not enough in this case. What you would want is for threads to cooperate by signalling each other (because you want main to not increment the variable until a thread says that it is done printing it). You can use a pthread_cond_t to achieve this (Look here to learn more about this). This boils down to the following code (basically, I added an appropriate usage of pthread_cond_t to your code, and a bunch of comments explaining what is going on):
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct params {
pthread_mutex_t mutex;
pthread_cond_t done;
int id;
};
typedef struct params params_t;
void* hello(void* arg){
int id;
/* Lock. */
pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
/* Work. */
id = (*(params_t*)(arg)).id;
printf("Hello from %d\n", id);
/* Unlock and signal completion. */
pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
pthread_cond_signal (&(*(params_t*)(arg)).done);
/* After signalling `main`, the thread could actually
go on to do more work in parallel. */
}
int main() {
pthread_t threads[10];
params_t params;
pthread_mutex_init (¶ms.mutex , NULL);
pthread_cond_init (¶ms.done, NULL);
/* Obtain a lock on the parameter. */
pthread_mutex_lock (¶ms.mutex);
int i;
for(i = 0; i < 10; i++) {
/* Change the parameter (I own it). */
params.id = i;
/* Spawn a thread. */
pthread_create(&threads[i], NULL, hello, ¶ms);
/* Give up the lock, wait till thread is 'done',
then reacquire the lock. */
pthread_cond_wait (¶ms.done, ¶ms.mutex);
}
for(i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
/* Destroy all synchronization primitives. */
pthread_mutex_destroy (¶ms.mutex);
pthread_cond_destroy (¶ms.done);
return 0;
}
I see that the example you are trying is a toy program to probably learn about the POSIX thread library. In the real world, as we all know this can be done much faster without even using threads. But you already know this.
The problem is that you are modifying the params.id "unprotected" in main. This modification in main also needs to be mutex protected. You could protect this access by localizing this by creating getId() and setId() functions that would lock the mutex and protect access to the id, as follows. This will most likely still give the problem reported, since depending on when the thread calls getData() it will have one value or another. So to solve this, you could add an incrementId() function and call it from the hello() function.
struct params {
pthread_mutex_t mutex;
int id;
};
typedef struct params params_t;
int getId(params_t *p)
{
int id;
pthread_mutex_lock(&(p->mutex));
id = p->id;
pthread_mutex_unlock(&(p->mutex));
return id;
}
void setId(params_t *p, int val)
{
pthread_mutex_lock(&(p->mutex));
p->id = val;
pthread_mutex_unlock(&(p->mutex));
}
void incrementId(params_t *p)
{
pthread_mutex_lock(&(p->mutex));
p->id++;
pthread_mutex_unlock(&(p->mutex));
}
void* hello(void* arg){
params_t *p = (params_t*)(arg);
incrementId(p);
int id = getId(p);
// This could possibly be quite messy since it
// could print the data for multiple threads at once
printf("Hello from %d\n", id);
}
int main() {
pthread_t threads[10];
params_t params;
params.id = 0;
pthread_mutex_init (¶ms.mutex , NULL);
int i;
for(i = 0; i < 10; i++) {
if(pthread_create(&threads[i], NULL, hello, ¶ms));
}
for(i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
A better way to get a unique thread id would be to define the hello method as follows:
void* hello(void* arg){
pthread_t threadId = pthread_self();
printf("Hello from %d\n", threadId);
}
And to avoid the problem with all threads trying to print at once, you could do the following:
void* hello(void* arg){
params_t *p = (params_t*)(arg);
pthread_mutex_lock(&(p->mutex));
p->id++;
int id = p->id;
printf("Hello from %d\n", id);
pthread_mutex_unlock(&(p->mutex));
}
Easiest way to get the desired output would be to modify your main function as follows:
int main() {
pthread_t threads[10];
params_t params;
pthread_mutex_init (¶ms.mutex , NULL);
int i;
for(i = 0; i < 10; i++) {
params.id = i;
if(pthread_create(&threads[i], NULL, hello, ¶ms));
pthread_join(threads[i], NULL); //wait for thread to finish
}
/*for(i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}*/
return 0;
}
Output would be:
Hello from 0
...
Hello from 9
EDIT: Here's the synchronization for the corrected question:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct params {
pthread_mutex_t* mutex;
int id;
};
typedef struct params params_t;
void* hello(void* arg){
int id = 0;
params_t* params = (params_t*)arg;
if(params != 0)
{
id = params->id;
delete params;
params = 0;
}
printf("Hello from %d\n", id);
}
int main() {
pthread_t threads[10];
params_t* params = 0;
pthread_mutex_t main_mutex;
pthread_mutex_init (&main_mutex , NULL);
int i;
for(i = 0; i < 10; i++) {
params = new params_t(); //create copy of the id to pass to each thread -> each thread will have it's own copy of the id
params->id = i;
params->mutex = &main_mutex;
if(pthread_create(&threads[i], NULL, hello, params));
}
for(i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
Each thread must have it's own copy of the id so that the other threads do not modify the id before it is printed.
I'm just putting this one here to provide another solution to this problem - this one does not involve mutexes - no synchronization - no conditionals, etc.
The main dfference is that we are using pthread_detach to automatically release the thread's resources upon completion.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUMTHREADS 10
typedef struct params {
int id;
} params_t;
void* hello(void *arg)
{
params_t *p = (params_t*)arg;
int status;
status = pthread_detach(pthread_self());
if (status !=0 )
{
printf("detaching thread\n");
abort();
}
printf("Hello from %d\n", p->id);
free(p);
return NULL;
}
int main()
{
pthread_t thread;
params_t *par;
int i, status;
for (i=0; i<NUMTHREADS; i++)
{
par = (params_t*)malloc(sizeof(params_t));
if (par == NULL)
{
printf("allocating params_t");
abort();
}
par->id = i;
status = pthread_create(&thread, NULL, hello, par);
if (status != 0)
exit(1);
}
/* DO some more work ...*/
sleep(3);
exit(0);
}