C++ 为什么在多线程应用程序c++;

C++ 为什么在多线程应用程序c++;,c++,multithreading,synchronization,pthreads,mutex,C++,Multithreading,Synchronization,Pthreads,Mutex,我不熟悉多线程。 当我运行下面的程序时,它将输出为 Function1 Function2 1000...1001 但当我调试程序时,它会按预期输出 1000...1001 Function1 Function2 因此,我认为在直接运行时(无需调试)模式下,它会遇到一些同步问题。但有一件事让我困惑,我使用的是mutex,那么为什么会出现同步问题呢 请帮帮我 #include <iostream> #include <cstdlib> #include <pthr

我不熟悉多线程。 当我运行下面的程序时,它将输出为

Function1
Function2
1000...1001
但当我调试程序时,它会按预期输出

1000...1001
Function1
Function2
因此,我认为在直接运行时(无需调试)模式下,它会遇到一些同步问题。但有一件事让我困惑,我使用的是
mutex
,那么为什么会出现同步问题呢

请帮帮我

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

pthread_mutex_t myMutex;
void * function1(void * arg);
void * function2(void * arg);
void * function0(void * arg);
int count = 0;
const int COUNT_DONE = 10;

main()
{
  pthread_t thread1, thread2, thread0;
  pthread_mutex_init(&myMutex, 0);
  pthread_create(&thread0, NULL, &function0, NULL );
  pthread_create(&thread1, NULL, &function1, NULL );
  pthread_create(&thread2, NULL, &function2, NULL );
  pthread_join(thread0, NULL );
  pthread_join(thread1, NULL );
  pthread_join(thread2, NULL );
  pthread_mutex_destroy(&myMutex);
  return 0;
}

void *function1(void * arg)
{
  cout << "Function1\n";
}

void *function0(void *arg)
{
  int i, j;
  pthread_mutex_lock(&myMutex);
  for (i = 0; i <= 1000; i++)
  {
    for (j = 0; j <= 1000; j++)
    {
    }
  }
  cout << i << "..." << j << endl;
  pthread_mutex_unlock(&myMutex);
}

void *function2(void * arg)
{
  cout << "Function2\n";
}
#包括
#包括
#包括
使用名称空间std;
pthread_mutex_t myMutex;
void*function1(void*arg);
void*function2(void*arg);
void*function0(void*arg);
整数计数=0;
const int COUNT_DONE=10;
main()
{
pthread_t thread1、thread2、thread0;
pthread_mutex_init(&myMutex,0);
pthread_create(&thread0,NULL,&function0,NULL);
pthread_create(&thread1,NULL,&function1,NULL);
pthread_create(&thread2,NULL,&function2,NULL);
pthread_join(thread0,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_mutex_destroy(&myMutex);
返回0;
}
void*function1(void*arg)
{
cout“…出现一些同步问题”您在哪里看到问题

线程输出可以以任何顺序出现,因为线程在任何情况下都不同步

互斥仅在一个线程中使用

可能是一个预期的有效结果

以及:

1000
Function1
... 
Function2
1001
修改
function1()
function2()
如下:

void *function1(void * arg)
{
  pthread_mutex_lock(&myMutex);
  cout << "Function1\n";
  pthread_mutex_unlock(&myMutex);
}

void *function2(void * arg)
{
  pthread_mutex_lock(&myMutex);
  cout << "Function2\n";
  pthread_mutex_unlock(&myMutex);
}
void*function1(void*arg)
{
pthread_mutex_lock(&myMutex);

cout什么是空循环?编译器可能会删除它们。另外,请解释互斥锁应该如何执行ordering@Leeor:Mtex用于同步目的!您不应该在
function2
function1
上使用互斥锁吗?@RasmiRanjanNayak-只有在使用正确的情况下…@nims:现在我已经把互斥锁放进去了这两个函数都是。但是结果是一样的。我也将互斥应用于其他两个函数。但是结果也没有改变。你肯定不会得到第二种情况。互斥保护混合3个输出状态,但不影响它们的顺序。你可能会想一想互斥如何以及为什么会影响3个输出状态的顺序tputs.mutex不提供任何排序。它们只确保在同一时间只有一个线程持有对mutex的锁。请求对mutex进行锁的线程可以按任何顺序执行。请记住mutex是互斥的缩写。如果需要保证执行顺序,可能需要更复杂的锁方案利用条件变量。
void *function1(void * arg)
{
  pthread_mutex_lock(&myMutex);
  cout << "Function1\n";
  pthread_mutex_unlock(&myMutex);
}

void *function2(void * arg)
{
  pthread_mutex_lock(&myMutex);
  cout << "Function2\n";
  pthread_mutex_unlock(&myMutex);
}