using thread in Kdb

Dear KDB Users,

I try to achieve this in KDB using C extension: create a C extension
function and this function creates a working thread and return. The
working thread inserts data to KDB. The insert call returns “vd1” as
error message. My question is that is thread supported by KDB? Here is
my test code on windows:

Thanks,

dbtouch

// w32>cl /LD ..\c\a.c a.def q.lib
// l32>/usr/local/gcc-3.3.2/bin/gcc -shared ../c/a.c -o a.so
// s32>/usr/local/gcc-3.3.2/bin/gcc -G ../c/a.c -o a.so
#include <winsock2.h>
#include “k.h”
#include <windows.h>
#include <stdio.h>
#include
using namespace std;

HANDLE ghStartEvent;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
DWORD dwWaitResult;
K err;
int bulkSize=5;
K x=knk(7, ktn(KI, bulkSize), ktn(KC, bulkSize), ktn(KH, bulkSize),
ktn(KI, bulkSize), <br> ktn(KF, bulkSize), ktn(KS, bulkSize), ktn(KT, bulkSize));
int i=0;
int count=0;

dwWaitResult = WaitForSingleObject(
ghStartEvent, // event handle
INFINITE); // indefinite wait

if (dwWaitResult==WAIT_OBJECT_0)
{
ResetEvent(ghStartEvent);

while (count<500)
{
// commandMap.dump2K(ptr, x);

kI(xK[0])[i] = count;
kC(xK[1])[i] = ‘a’;
kH(xK[2])[i] = 100;
kI(xK[3])[i] = 1000;
kF(xK[4])[i] = 99.9;
kS(xK[5])[i] = (S)“hello world”;
kI(xK[6])[i] = kt(200)->i;
i++;

if (i==bulkSize)
{
err=k(0, “insert”, ks((S)“all_dt”), x, (K)0);
if (err->t == -128)
{
O(“err = %s \n”, err->s);
break;
}
i=0;
}
}
}
return 0;
}

K startAud(K x)
{
WSADATA wsaData;
int iResult;
DWORD dwThreadID;
string dirName=“C:/kdb+espd/test/CFG”;
K err;

ghStartEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
TEXT(“StartEvent”) // object name
);

dwThreadID=(DWORD)CreateThread(
NULL, // default security
0, // default stack size
ThreadProc, // name of the thread function
NULL, // no thread parameters
0, // default startup flags
&dwThreadID);

Sleep(3);
SetEvent(ghStartEvent);
return ki(x->i+1);
}



</stdio.h></windows.h></winsock2.h>

Looking at https://code.kx.com/trac/wiki/errors I noted this entry:vd1 attempted multithread updateAnother page on the wiki (https://code.kx.com/trac/wiki/Releases/ChangesIn2.4) provided some more hints - the section “multi-threadedinput”:"Although 2.3 used multithreading to farm out queries to multiplepartitions using the number of threads set via the -s parameter theinput queue was still single threaded. In 2.4 it is possible tomultithread that queue as well. […]q .. -p -5001 / multithreaded input queue"So, if you haven’t tried it, I’d recommend using a negative parameterto ‘-p’ and see if it works out… Can’t say for certain as I haven’treally worked with threaded input in extensions.H?kan

Hi, H?kanThank you for the information. My question is not exactly onmultithreaded input. I actually create a daemon thread in q and in thedaemon thread I insert into q. So the k handle I use is 0, because mycall is part of q process.YE LIUOn Sep 30, 5:22?pm, Lindqvist <lindqvist.h…> wrote:> Looking at ?https://code.kx.com/trac/wiki/errors?I noted this entry:>> vd1 ? ? ? ? ? ? attempted multithread update>> Another page on the wiki (https://code.kx.com/trac/wiki/Releases/&gt; ChangesIn2.4) provided some more hints - the section “multi-threaded> input”:>> “Although 2.3 used multithreading to farm out queries to multiple> partitions using the number of threads set via the -s parameter the> input queue was still single threaded. In 2.4 it is possible to> multithread that queue as well.> ? […]> q .. -p -5001 / multithreaded input queue”>> So, if you haven’t tried it, I’d recommend using a negative parameter> to ‘-p’ and see if it works out… Can’t say for certain as I haven’t> really worked with threaded input in extensions.>> H?kan</lindqvist.h…>

> […] My question is not exactly on> multithreaded input. I actually create a daemon thread in q and in the> daemon thread I insert into q. So the k handle I use is 0, because my> call is part of q process […]I may have misunderstood, but this is my interpretation of the issueat that time (if someone has proper intel, please go ahead and set anymisconceptions straight):Since you are trying to do inserts from a “non-main” thread, Qcomplains. I thought that if you perhaps changed to a negativeparameter to -p it would go away.Since you are using handle 0 to do the inserts one might believe thatit would enter the input queue using the “standard channel”, but itmay very well be possible that Q optimizes and will let the currentthread (your worker thread) be the one executing actual insertionoperations.These are just guesses though - you might very well have tested theflagging I suggested and it failed for all I know…H?kan