How to coordonate threads in C
I have a little issue and I don't understand why my code doesn't work.
The purpose of the code is to use 3 different threads to display the sequence:
(abc)*
The output shows only one 'A' ...
sem_t mutex;
char turn = 'a';
void displayA()
{
for(int i = 0; i < 100; i++)
{
sem_wait(&mutex);
if (turn == 'a')
{
printf("A");
turn = "b";
printf("%c", turn);
}
sem_post(&mutex);
}
return NULL;
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t thread1_id;
pthread_t thread2_id;
pthread_t thread3_id;
pthread_create(&thread1_id, NULL, displayA, NULL);
pthread_create(&thread2_id, NULL, displayB, NULL);
pthread_create(&thread3_id, NULL, displayC, NULL);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
pthread_join(thread3_id, NULL);
//sem_destroy(&mutex);
printf("n END mainn");
return 0;
}
Thanks you for any help
c multithreading pthreads mutex semaphore
|
show 4 more comments
I have a little issue and I don't understand why my code doesn't work.
The purpose of the code is to use 3 different threads to display the sequence:
(abc)*
The output shows only one 'A' ...
sem_t mutex;
char turn = 'a';
void displayA()
{
for(int i = 0; i < 100; i++)
{
sem_wait(&mutex);
if (turn == 'a')
{
printf("A");
turn = "b";
printf("%c", turn);
}
sem_post(&mutex);
}
return NULL;
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t thread1_id;
pthread_t thread2_id;
pthread_t thread3_id;
pthread_create(&thread1_id, NULL, displayA, NULL);
pthread_create(&thread2_id, NULL, displayB, NULL);
pthread_create(&thread3_id, NULL, displayC, NULL);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
pthread_join(thread3_id, NULL);
//sem_destroy(&mutex);
printf("n END mainn");
return 0;
}
Thanks you for any help
c multithreading pthreads mutex semaphore
1
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
1
The code you posted would not compile because it is missingdisplayB
anddisplayC
. Also,turn = "b"
will be flagged. Please post your full code that compiles and links cleanly
– Craig Estey
Nov 18 '18 at 18:10
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
1
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
The posted code does not compile! Amongst other things, it is missing the#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?
– user3629249
Nov 18 '18 at 23:22
|
show 4 more comments
I have a little issue and I don't understand why my code doesn't work.
The purpose of the code is to use 3 different threads to display the sequence:
(abc)*
The output shows only one 'A' ...
sem_t mutex;
char turn = 'a';
void displayA()
{
for(int i = 0; i < 100; i++)
{
sem_wait(&mutex);
if (turn == 'a')
{
printf("A");
turn = "b";
printf("%c", turn);
}
sem_post(&mutex);
}
return NULL;
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t thread1_id;
pthread_t thread2_id;
pthread_t thread3_id;
pthread_create(&thread1_id, NULL, displayA, NULL);
pthread_create(&thread2_id, NULL, displayB, NULL);
pthread_create(&thread3_id, NULL, displayC, NULL);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
pthread_join(thread3_id, NULL);
//sem_destroy(&mutex);
printf("n END mainn");
return 0;
}
Thanks you for any help
c multithreading pthreads mutex semaphore
I have a little issue and I don't understand why my code doesn't work.
The purpose of the code is to use 3 different threads to display the sequence:
(abc)*
The output shows only one 'A' ...
sem_t mutex;
char turn = 'a';
void displayA()
{
for(int i = 0; i < 100; i++)
{
sem_wait(&mutex);
if (turn == 'a')
{
printf("A");
turn = "b";
printf("%c", turn);
}
sem_post(&mutex);
}
return NULL;
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t thread1_id;
pthread_t thread2_id;
pthread_t thread3_id;
pthread_create(&thread1_id, NULL, displayA, NULL);
pthread_create(&thread2_id, NULL, displayB, NULL);
pthread_create(&thread3_id, NULL, displayC, NULL);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
pthread_join(thread3_id, NULL);
//sem_destroy(&mutex);
printf("n END mainn");
return 0;
}
Thanks you for any help
c multithreading pthreads mutex semaphore
c multithreading pthreads mutex semaphore
asked Nov 18 '18 at 17:45
John DoeJohn Doe
386
386
1
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
1
The code you posted would not compile because it is missingdisplayB
anddisplayC
. Also,turn = "b"
will be flagged. Please post your full code that compiles and links cleanly
– Craig Estey
Nov 18 '18 at 18:10
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
1
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
The posted code does not compile! Amongst other things, it is missing the#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?
– user3629249
Nov 18 '18 at 23:22
|
show 4 more comments
1
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
1
The code you posted would not compile because it is missingdisplayB
anddisplayC
. Also,turn = "b"
will be flagged. Please post your full code that compiles and links cleanly
– Craig Estey
Nov 18 '18 at 18:10
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
1
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
The posted code does not compile! Amongst other things, it is missing the#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?
– user3629249
Nov 18 '18 at 23:22
1
1
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
1
1
The code you posted would not compile because it is missing
displayB
and displayC
. Also, turn = "b"
will be flagged. Please post your full code that compiles and links cleanly– Craig Estey
Nov 18 '18 at 18:10
The code you posted would not compile because it is missing
displayB
and displayC
. Also, turn = "b"
will be flagged. Please post your full code that compiles and links cleanly– Craig Estey
Nov 18 '18 at 18:10
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
1
1
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
The posted code does not compile! Amongst other things, it is missing the
#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?– user3629249
Nov 18 '18 at 23:22
The posted code does not compile! Amongst other things, it is missing the
#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?– user3629249
Nov 18 '18 at 23:22
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363785%2fhow-to-coordonate-threads-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363785%2fhow-to-coordonate-threads-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you want to associate each character to one thread or you just want a thread to print a letter? If it's the second one you just need a simple lock and a character that represents what to print. You acquire the lock, see what you need to print, release the lock. You can also do it (easily) with semaphores. You must also implement the logic to print all other characters in your sequence
– simo-r
Nov 18 '18 at 17:58
1
The code you posted would not compile because it is missing
displayB
anddisplayC
. Also,turn = "b"
will be flagged. Please post your full code that compiles and links cleanly– Craig Estey
Nov 18 '18 at 18:10
I couldn't post all my code because it was too long, but the functions displayB and displayC exist:
– John Doe
Nov 18 '18 at 19:13
1
@JohnDoe Posting code that does not compile cleanly is a red flag, indicating to the reader that you are already misinterpreting what you think you're seeing. Post a Minimal, Complete, and Verifiable example. The stackoverflow interface should allow you to post your minimal example program.
– Gil Hamilton
Nov 18 '18 at 22:13
The posted code does not compile! Amongst other things, it is missing the
#include
statements needed for the needed header files. Are you expecting us to guess as to which header files are needed?– user3629249
Nov 18 '18 at 23:22