| |

VerySource

 Forgot password?
 Register
Search
View: 755|Reply: 7

linux multithreaded programming

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-11 11:30:01
| Show all posts |Read mode
A procedure
pthread_t thread [5];
pthread_mutex_t mut;
pthreat_cond_t cond
.
void Creat_Thread (char * p)
{
 pthread_create (&thread [n], NULL, thread1, (void *) p);
}

void * thread1 (void * p)
{
  .....
  while (1)
{
pthread_mutex_lock (&mut);
pthread_cond_wait (&cond,&mut);
.......... processing function 1;
Processing function 2;
Processing function 3;
Processing function 4;
pthread_mutex_unlock (&mut);
}
pthread_exit (NULL);
}
.
.
int main ()
{
  Initialize mutex and condition variables;
. . . .
 for (i = 0; i <5; i ++)
     Creat_Thread (p [i]);
do
{
  pthread_cancel (thread [4]);
  pthread_cond_signal (&cond); // Send a signal
 } while (1);
 thread_wait ();
 return 0;
}
The program compiles. Now the problem is that when the multi-threaded program is running, when the thread is created (that is, when the for loop creates the thread), it runs correctly (all processing functions run correctly), but then it runs using the mutex and condition variables. When all threads run to processing function 3, processing function 4 is not running; I don't know what the reason is;
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-5 11:15:01
| Show all posts
Look at the logical process of the program...
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-5 12:30:02
| Show all posts
void Creat_Thread (char* p)
{
pthread_create(&thread[n], NULL, thread1, (void *)p);
}

Where did n come from? Is there a missing parameter?
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-6-8 01:30:01
| Show all posts
The program logic is executed sequentially,
Is Creat_Thread(char* p, int n)
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-9 21:15:01
| Show all posts
Print more information
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-14 12:30:01
| Show all posts
Which thread sends pthread_cond_signal?
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-15 15:30:01
| Show all posts
When pthread[4] is canceled, the thread may not release the mutex,
Because pthread_cond_signal is a cancel point, the thread may end here
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-5 11:15:01
| Show all posts
In addition, before calling pthread_cond_signal, you must add a mutex, otherwise,
It may be because pthread_cond_wait loses protection when comparing test conditions with memory
And the order is chaotic
After calling pthread_cond_signal, unlock
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list