C语言多线程操作多函数 冲突

问题遇到的现象和发生背景

通过移动光标的方式,用两个线程执行两个不同函数分别在某列输出不同的数字。由于线程并发,输出的结果不对

问题相关代码,请勿粘贴截图
#include <pthread.h>
#include<stdio.h>
#include<windows.h>
void gotoxy(int x, int y){
    COORD pos;
    HANDLE hOutput;
    pos.X = x;
    pos.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, pos);
}
void * fun1(void * a){
    int t=0;
    while(t<20){
        Sleep(200);
        gotoxy(10,t++);
        printf("11");
    }
}
void* fun2(void *a){
    int t=0;
    while(t<20){
        Sleep(200);
        gotoxy(1,t++);
        printf("22");
    }
}
int main(){
    pthread_t pthread1,pthread2;
    pthread_create(&pthread1,NULL,&fun1,NULL);
    pthread_create(&pthread2,NULL,&fun2,NULL);
    
    pthread_join(pthread1,NULL);
    pthread_join(pthread2,NULL);
}

运行结果及报错内容
      2211

22 11
2211
1122
22 11
2211
2211
22 11
22 11
22 11
22 11
22 11
22 11
22 11
22 11
22 11
22 11
22 11
2211
1122

我的解答思路和尝试过的方法
我想要达到的结果

因为你的线程里面执行的不是原子操作
就是挪动光标和打印是两个不同的过程
那么就有可能线程1刚挪好了光标,没等打印,线程2又挪了光标,然后线程1打印,线程2打印
或者反过来,先2后1再2再1,谁先谁后是随机的
你想让过程变成原子的,需要加线程同步锁

#include <pthread.h>
#include <stdio.h>
#include <windows.h>

pthread_mutex_t m;

void gotoxy(short x, short y)
{
    COORD pos;
    HANDLE hOutput;
    pos.X = x;
    pos.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    pthread_mutex_lock(&m);
    SetConsoleCursorPosition(hOutput, pos);
}

void *fun1(void *unused)
{
    short t = 0;
    while (t < 20)
    {
        gotoxy(10, t++);
        printf("11");
        pthread_mutex_unlock(&m);
    }
    pthread_exit(NULL);
    return NULL;
}

void *fun2(void *unused)
{
    short t = 0;
    while (t < 20)
    {
        gotoxy(1, t++);
        printf("22");
        pthread_mutex_unlock(&m);
    }
    pthread_exit(NULL);
    return NULL;
}

int main()
{
    pthread_mutex_init(&m, NULL);

    pthread_t pthread1;
    pthread_t pthread2;

    pthread_create(&pthread1, NULL, &fun1, NULL);
    pthread_create(&pthread2, NULL, &fun2, NULL);

    pthread_join(pthread1, NULL);
    pthread_join(pthread2, NULL);

    pthread_mutex_destroy(&m);

    return 0;
}